package me.xiaoyan.android.tuition.activity;

import java.util.HashMap;
import java.util.Map;

import me.xiaoyan.android.tuition.R;
import me.xiaoyan.android.tuition.utils.AppManager;
import me.xiaoyan.android.tuition.utils.SESHttp;
import me.xiaoyan.android.tuition.utils.TuitionException;
import me.xiaoyan.android.widget.BaseUtil;
import me.xiaoyan.android.widget.DataBackListener;
import me.xiaoyan.android.widget.YLog;
import me.xiaoyan.json.JSONArray;
import me.xiaoyan.json.JSONObject;
import android.app.ProgressDialog;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Common implements OnClickListener {
	private static final String TAG = "Activity.Register";

	private EditText txtPhone;
	private EditText txtPass;
	private EditText txtPassRepeat;
	private EditText txtsmsCode;
	private Button btnSendSMS;
	private Button btnSigup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		txtPhone = (EditText) findViewById(R.id.txt_reg_phone);
		txtPass = (EditText) findViewById(R.id.txt_reg_password);
		txtPassRepeat = (EditText) findViewById(R.id.txt_reg_password_repeat);
		txtsmsCode = (EditText) findViewById(R.id.txt_reg_smscode);

		btnSigup = (Button) findViewById(R.id.btn_reg_sigup);
		btnSendSMS = (Button) findViewById(R.id.btn_reg_sendsms);

		btnSigup.setOnClickListener(this);
		btnSendSMS.setOnClickListener(this);
		
		showBackButton();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_reg_sendsms) {
			if (BaseUtil.stringIsEmpty(txtPhone.getText().toString())) {
				showToast(getString(R.string.username_is_null),
						Toast.LENGTH_SHORT);
			} else {
				sendSMS();
			}
		} else if (v.getId() == R.id.btn_reg_sigup) {
			if (BaseUtil.stringIsEmpty(txtPhone.getText().toString())) {
				showToast(getString(R.string.username_is_null),
						Toast.LENGTH_SHORT);
			} else if (BaseUtil.stringIsEmpty(txtPass.getText().toString())) {
				showToast(getString(R.string.password_is_null),
						Toast.LENGTH_SHORT);
			} else if (BaseUtil.stringIsEmpty(txtPassRepeat.getText()
					.toString())) {
				showToast(getString(R.string.password_repeat_is_null),
						Toast.LENGTH_SHORT);
			} else if (!txtPassRepeat.getText().toString()
					.equals(txtPass.getText().toString())) {
				showToast(getString(R.string.password_repeat_not_eq),
						Toast.LENGTH_SHORT);
			} else if (BaseUtil.stringIsEmpty(txtsmsCode.getText().toString())) {
				showToast(getString(R.string.smscode_is_null),
						Toast.LENGTH_SHORT);
			} else {
				userSigup();
			}
		}
	}

	private void sendSMS() {
		if (!BaseUtil.isPhoneNumber(txtPhone.getText().toString())) {
			showToast(getString(R.string.tap_right_phone), Toast.LENGTH_SHORT);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("loginAccount", txtPhone.getText().toString());
		param.put("phone", txtPhone.getText().toString());

		SESHttp.sendRequest(SEND_SMSCODE, param, new DataBackListener() {

			@Override
			public void success(String source) {
				try {
					JSONObject json = new JSONObject(source);
					if (json.optInt("code") != 0) {
						throw new Exception(json.getString("msg") + "("
								+ json.optInt("code") + ")");
					} else {
						showToast(getString(R.string.send_sms_success),
								Toast.LENGTH_SHORT);
					}
				} catch (Exception e) {
					showToast(e.getMessage(), Toast.LENGTH_SHORT);
				}
			}

			@Override
			public void failed(TuitionException e) {
				YLog.e(TAG, e);
			}
		});
	}

	private void userSigup() {
		if (!BaseUtil.isPhoneNumber(txtPhone.getText().toString())) {
			showToast(getString(R.string.tap_right_phone), Toast.LENGTH_SHORT);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();

		param.put("loginAccount", txtPhone.getText().toString());
		param.put("phone", txtPhone.getText().toString());
		param.put("pwd", txtPass.getText().toString());
		param.put("verifyCode", txtsmsCode.getText().toString());

		param.put("question", "1");
		param.put("answer", "1");
		param.put("channelNo", "1");
		param.put("recommendType", "");
		param.put("recommender", "");
		param.put("imei", BaseUtil.getIMEI(this));
		try {
			param.put("version", BaseUtil.getApplicationVersion(this));
		} catch (NameNotFoundException e1) {
			param.put("version", "1.0");
		}
		final ProgressDialog pd = getProgress(
				getString(R.string.loading_sigup_data), false);
		SESHttp.sendRequest(USER_REGISTER, param, new DataBackListener() {

			@Override
			public void success(String source) {
				pd.dismiss();
				System.out.println("DataBackListener" + source);
				try {
					JSONArray json = checkSESData(source);
					String customerid = json.getJSONObject(0).optString(
							"customerId");
					AppManager.getInstance().saveAppDatas(CUSTOMER_ID,
							customerid);

					goback();
				} catch (Exception e) {
					showToast(e.getMessage(), Toast.LENGTH_SHORT);
				}
			}

			@Override
			public void failed(TuitionException e) {
				pd.dismiss();
				YLog.e(TAG, e);
			}
		});
	}

	@Override
	public void onBackPressed() {
		super.goback();
	}
}
